-2

I am seeking to create a reusable "employee lookup" control.

Note:I am assuming that a partial view is the best way to go.

  1. I want multiple buttons on the page
  2. Each button will call a PartialView and each button will have a specific textbox
  3. Each partial will contain multiple Results (items)
  4. On clicking one of the results I want to populate the button's textbox, that made the call, with the result

How am i able to do this, since the page will have multiple buttons and textboxes?

Matt Hampel
  • 5,088
  • 12
  • 52
  • 78
BeYourOwnGod
  • 2,345
  • 7
  • 30
  • 35
  • Downvoters...any advice on how I can make this a better question? – BeYourOwnGod Jun 15 '16 at 16:44
  • Not a downvoter but formatting a little better and adding some examples of what you have and what you have tried is the general advice given in this situation – Charleh Jun 15 '16 at 16:49

1 Answers1

1

This control needs to be able to be called by multiple buttons

So, those buttons call an action which will render the partial which has those results?

I'm seeing multiple ways to do this. The easiest way is:

<button id="btn1" class="btns" data-target="txt1" type="button">A</button>
<button id="btn2" class="btns" data-target="txt2" type="button">B</button>

<input type="text" id="txt1" />
<input type="text" id="txt2" />

<div id="render">

</div>

<script>
    var ajaxActive = false;

    $(function() {
        $(".btns").on('click', function () { // Bind the onclick of the button, so any button with this class may call the following function
            var _button = $(this);
            getItems(_button);
        });
    });

    function getItems(_button) {
        var bind = function (_button, results) {
            $("#render").empty();
            $("#render").append(results); // Append the partialview to the current view's div

            $("#render .itemResult").on('click', function () { // Bind the onclick of the result
                var resultValue = $(this).text(); // Or any other value that come from the result
                var targetId = "#" + _button.data('target'); // Id of the input (Target) which comes from the clicked button

                $(targetId).val(resultValue); // Change the input target value with the result one
            });
        };

        if (ajaxActive) {
            $.get('/Controller/Action') // Get the partialview 
           .done(function (results) {
               bind(_button, results);
           });
        }
        else {
            var results = simulateCall(); // Get the results
            bind(_button, results);
        }
    }

    function simulateCall() { // Simulate a call to the server
        return "<div class='items'> <div class='itemResult'>ABC</div> <div class='itemResult'>DEF</div> </div>";
    }
</script>

PS: Here is a working demo

Keep in mind that i placed some sort of "call" to simulate it going to the database

Leandro Soares
  • 2,902
  • 2
  • 27
  • 39
  • Leandro, thank you for your time. This is close to what I am looking for. I believe I can work with and adjust this to meet my needs. I assume that what you shared above would be in the partial view and buttons A and B would be in my view? My real world usage of this would be a view where potentially more than one employee would be chosen (ex: employee, and then supervisor). – BeYourOwnGod Jun 15 '16 at 17:19
  • I would recommend creating a script where you put all the JS you'll use for this control. Then the rest will be in your partialview. The main view would only call (JS) something like `initEmployeeControl();` – Leandro Soares Jun 15 '16 at 17:23
  • The buttons will stay on the main view if the buttons change from view to view. Or if the buttons are equal on all the views, then set it on the partial view. – Leandro Soares Jun 15 '16 at 17:24